package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.PayCycleAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@AttrHandler(PayCycleAttrHandler.class)
public class PayCycleAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Override
    public void handle(KiteAttr kiteAttr, KiteInst inst, AttrData attrData) {
        // 支付周期选择“月”  计收方式展示必填
        String newAddType = inst.getAttrValue("200003796");
        KiteAttr speedAttr = inst.getAttr("210004133");
        if (KiteStringUtils.equals(newAddType, "100002333")) {
            if (!KiteObjectUtils.isEmpty(speedAttr)) {
                speedAttr.setIsNull(KeyConsts.IFTRUE_F);
                LocalAttrHelper.hiddenOrDisplayProdAttr(attrData.getSceneInstId(), inst, speedAttr, KeyConsts.PROD_ATTR_SHOW_TYPE_D, speedAttr.getDefaultValue());
            }
        } else {
            if (Objects.nonNull(speedAttr)) {
                LocalAttrHelper.hiddenOrDisplayProdAttr(attrData.getSceneInstId(), inst, speedAttr, KeyConsts.PROD_ATTR_SHOW_TYPE_H, "");
            }
        }

        //支付周期为年时，去掉获取自动续约下拉框中的 按月自动续约
        //获取自动续约下拉框值
        KiteAttr attr1 = inst.getAttr("210008175");
        if(Objects.nonNull(attr1)) {
            if ("100002481".equals(kiteAttr.getValue())) {
                //支付周期为年时，去掉获取自动续约下拉框中的 按月自动续约
                attr1.getAttrValues().removeIf(targetAttr -> KiteStringUtils.equals(targetAttr.getAttrValue(), "110015557"));

            } else {
                KiteAttrValue kiteAttrValue = attr1.getAttrValues().stream().filter(targetAttr -> KiteStringUtils.equals(targetAttr.getAttrValue(), "110015557")).findFirst().orElse(null);
                if (!Objects.nonNull(kiteAttrValue)) {
                    List<AttrValue> attrValues = AttrValueCache.get(attr1.getAttrId());
                    if(KiteListUtils.isNotEmpty(attrValues)){
                        attr1.setAttrValues(KiteBeanUtils.copyPropsForList(attrValues, KiteAttrValue.class));
                    }
                }
            }
            //设置默认值为否
            attr1.setDefaultValue("1_0000107");
            attr1.setValue("1_0000107");
            attr1.setValueDesc("否");
        }
    }

    @Override
    public void handleStyle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {

    }
}
